package com.jiangnan.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jiangnan.databinding.ItemTransactionLogBinding
import com.jiangnan.entity.PaymentLog
import java.text.SimpleDateFormat
import java.util.*

class TransactionLogAdapter : RecyclerView.Adapter<TransactionLogAdapter.VH>() {

  private val list = mutableListOf<PaymentLog>()
  private val fmt = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())

  class VH(val binding: ItemTransactionLogBinding) : RecyclerView.ViewHolder(binding.root)

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
    val b = ItemTransactionLogBinding.inflate(
      LayoutInflater.from(parent.context),
      parent, false
    )
    return VH(b)
  }

  override fun getItemCount() = list.size

  override fun onBindViewHolder(holder: VH, position: Int) {
    val it = list[position]
    holder.binding.tvRemark.text = it.remark
    holder.binding.tvAmount.text = 
      if (it.amount.signum() >= 0) "+${it.amount}" else it.amount.toString()
    holder.binding.tvBalanceAfter.text = "余额：${it.balanceAfter}"
    holder.binding.tvTime.text = fmt.format(it.createdAt)
  }

  fun submit(data: List<PaymentLog>) {
    list.clear()
    list.addAll(data)
    notifyDataSetChanged()
  }
}